package com.ruoyi.web.controller.shopping.backend;

import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.shopping.Category;
import com.ruoyi.system.domain.shopping.User;
import com.ruoyi.system.service.shopping.CategoryService;
import com.ruoyi.system.view.common.Result;
import com.ruoyi.system.view.common.ResultList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/backend/shopping/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultList<List<Category>> list(Category category){
        startPage();
        List<Category> categories = categoryService.selectCategoryList(category);
        return ResultList.success(categories, (int) new PageInfo(categories).getTotal(),"查询成功");
    }
    @PostMapping
    public Result add(@RequestBody Category category){
        Boolean add = categoryService.add(category);
        return Result.success(null,add ? "添加成功":"添加失败");
    }
    @GetMapping("/{categoryId}")
    public Result<Category> get(@PathVariable Integer categoryId){
        Category category = categoryService.selectCategoryById(categoryId);
        return Result.success(category,"查询成功");
    }
    @PutMapping
    public Result update(@RequestBody Category category)
    {
        Boolean aBoolean = categoryService.update(category);
        if(aBoolean){
            return Result.success(null,"操作成功");
        }
        return Result.error(null,201,"操作失败");
    }
    @DeleteMapping("/{categoryIds}")
    public Result delete(@PathVariable Integer[] categoryIds){
        Boolean aBoolean = categoryService.deleteByIds(categoryIds);
        if(aBoolean){
            return Result.success(null,"操作成功");
        }
        return Result.error(null,201,"操作失败");
    }
}
